package com.zmn.oms.services.impl.shop;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.shop.OrderShop;
import com.zmn.oms.persistence.interfaces.shop.OrderShopDao;
import com.zmn.oms.services.interfaces.shop.OrderShopService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class OrderShopServiceImpl implements OrderShopService {

    @Resource
    OrderShopDao orderShopDao;

    @Override
    public OrderShop findByKeySrcMaster(Long shopOrderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderShopDao.findByKey(shopOrderId);
        }
    }

    @Override
    public OrderShop findByKey(Long shopOrderId) {
        return orderShopDao.findByKey(shopOrderId);
    }

    @Override
    public List<OrderShop> listOrderShopByOrderIdList(List<Long> orderIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderShopDao.listOrderShopByOrderIdList(orderIdList);
        }
    }

    @Override
    public int insert(OrderShop orderShop) {
		orderShop.setCreateTime(Optional.ofNullable(orderShop.getCreateTime()).orElse(DateUtil.getNow()));
        return orderShopDao.insert(orderShop);
    }
}
